package com.whd.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whd.demo.common.Result;
import com.whd.demo.entity.Owner;
import com.whd.demo.entity.User;
import com.whd.demo.mapper.OwnerMapper;
import com.whd.demo.mapper.UserMapper;
import com.whd.demo.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/owner")
public class OwnerController {

    @Resource
    OwnerMapper ownerMapper;

    /**
     * 新增房主
     * @param owner
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody Owner owner){
        ownerMapper.insert(owner);
        return Result.success();
    }

    /**
     * 用户登陆
     * @param owner
     * @return
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody Owner owner){
        Owner res = ownerMapper.selectOne(Wrappers.<Owner>lambdaQuery().eq(Owner::getName, owner.getName()).eq(Owner::getPassword, owner.getPassword()));
        if (res == null) {
            return Result.error("-1","用户名或密码错误");
        }
        return Result.success(res);
    }

    /**
     * 用户注册
     * @param owner
     * @return
     */
    @PostMapping("/register")
    public Result<?> register(@RequestBody Owner owner){
        Owner res = ownerMapper.selectOne(Wrappers.<Owner>lambdaQuery().eq(Owner::getName, owner.getName()));
        if (res != null) {
            return Result.error("-1","用户名已存在");
        }
        if (owner.getPassword() == null){
            owner.setPassword("123456");
        }
        ownerMapper.insert(owner);
        return Result.success();
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Owner> wrapper = Wrappers.<Owner>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Owner::getName,search);
        }
        Page<Owner> userPage = ownerMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(userPage);
    }

    /**
     * 分页查询（查询除了管理员之外的用户信息）（包含了房屋表的一对多查询）
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping("/exceptAdmin")
    public Result<?> findPageExceptAdmin(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Owner> wrapper = Wrappers.<Owner>lambdaQuery().ne(Owner::getRole,0);
        if(StrUtil.isNotBlank(search)){
            wrapper.like(Owner::getName,search).ne(Owner::getRole,0);
        }
        Page<Owner> ownerPage = ownerMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(ownerPage);
    }

    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(ownerMapper.selectById(id));
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        ownerMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 更新用户
     * @param owner
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody Owner owner){
        ownerMapper.updateById(owner);
        return Result.success();
    }
}
